package tests.flat.files.scan;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.LineNumberReader;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;


public class TraillerSeparator {

	public TraillerSeparator() {
	}

	public static void main(String[] args) throws IOException, URISyntaxException {
		BufferedReader reader = Files.newBufferedReader(Paths.get("/home/java-eclipse/workspace/MusicBrain-Libraries/src/camara_japao.txt"), StandardCharsets.UTF_8);
		LineNumberReader lnr = new LineNumberReader (reader);
		String v = null ;
		int incrementer = 0; 
		StringBuilder acumulator = new StringBuilder();
		ArrayList<String> list = new ArrayList<String>();
		String record = "";
		while ((v = lnr.readLine())!=null)
		{   
				if (v.trim().equals("") && incrementer>0)
				{ 
					acumulator.insert(0, "<line>");
					acumulator.insert(acumulator.length(), "</line>");
					record = acumulator.toString();
					list.add(record);
					acumulator = new StringBuilder();
				}else if (!v.trim().equals("")){ 
					acumulator.append("|" + v + "|");
				}
				v = v.trim();
				incrementer++;
		}
		StringBuilder resultBuilder ; 
		ArrayList<String> lineRecord = new ArrayList<String>();
		for (String str : list) 
		{ 
			resultBuilder = new StringBuilder();
			resultBuilder.append("<p>");
//			StringTokenizer tokenizer = new StringTokenizer(str,"||");
//			while (tokenizer.hasMoreTokens()) 
//			{ 
//				System.out.println("<line>");
			resultBuilder.append(str);
//				System.out.println("</line>");
//			}
//			System.out.println(str);
			resultBuilder.append("</p>");
			String line = resultBuilder.toString(); 
			line = line.replace("||", "</cell><cell>");
			line = line.replace("|", "<cell>");
			System.out.println(line);
			lineRecord.add(line);
		}
		File outFile = new File("/home/java-eclipse/records.results.xml");
		if (!outFile.exists())
				outFile.createNewFile();
		else 
		{ 
			outFile.delete(); 
			outFile.createNewFile();
		}
		FileWriter fileWriter = new FileWriter(outFile);
		fileWriter.write("<root>");
		for (String lineResult : lineRecord)
		{ 
			fileWriter.write(lineResult);
		}
		fileWriter.write("</root>");
		fileWriter.close();
		//		System.out.println("</p>");
	}

}
